<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>UMS任任务调度系统</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<style type="text/css">
		*{padding: 0px;margin: 0px;}
	
		.taskList{width: 99%;margin:0px auto;overflow: auto; padding:5px; border:1px solid #ccc;}
	
		table{table-layout: fixed;margin:0px auto; border-collapse: collapse;border-spacing: 0px;}
		td,th{border: 1px solid #ccc;white-space:nowrap;padding:5px;text-align:left;}
		th{background-color: #f0f2ff;text-align: center;}
		td{font-size: 12px;}
		
		.taskMethodTable{margin: 0px;}
		.taskMethodTable td{padding:10px;}
		.notHaveDate{text-align: center;color: red;}
		
		tfoot tr td{text-align: center;background-color: #f0f2ff;}
		
		.bar{border: 1px solid #ccc; margin-bottom: 20px; padding:10px;}
		
		#log{width:100%;position:absolute;bottom:0px;min-height:400px;max-height:400px;overflow:auto;background-color:#000;color:#fff;z-index:9999;padding:10px 0px 10px 0px;}
	</style>
	<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.min.js"></script>
	<script type="text/javascript">
	$(function(){
		$(".taskList").css({
			"max-height":$(window).height()-411,
			"overflow":"auto"
		});
	});
	function execMethod(className,methodName){
		$.ajax({
	        type:"POST",
	        url:"servlet/TaskManagerServlet",
	        data:{
	        	cmd:"exec",
	        	className:className,
	        	methodName:methodName
        	},
	        dataType: "json",
	        success:function(data){
	        	console.log(data);
	       		if(data.resCode==200){
	       			console.log("执行成功!");
	       		}else{
	       			alert(data.resMsg);
	       		}
	        },
			error:function(){
				alert("服务器端异常!");
			}      
	     });
	}
	function showLog(){
		$("#log").slideToggle(100);
	}
	function maxShowConsole(){
		if($("#log").height()==400){
			$("#log").css({
				"max-height":($(window).height()-60)+"px",
				"height":$(window).height()+"px"
			});
		}else{
			$("#log").css({
				"max-height":"400px",
				"height":"400px"
			});
		}
	}
	window.lastTimeFileSize="";
	var isLoaded=false;
	var len=0;
	function loadLog(){
		$.ajax({
			type:"get",
	        url:"servlet/TaskManagerServlet",
			data:{
	        	cmd:"showLog",
	        	lastTimeFileSize:window.lastTimeFileSize
			},
			dataType:"json",
			success:function(data){
				if(data){
					window.lastTimeFileSize=data.lastTimeFileSize;
					console.log(window.lastTimeFileSize);
					if(data.content!=""){
						len+=data.content.length;
						console.log("len:"+len);
						if(len>50000){
							len=0;
							$("#log").html("");
						}
						$("#log").append("<pre>"+data.content+"</pre>");
						$("#log").scrollTop( $("#log")[0].scrollHeight );
					}
				}
			},
			error:function(){
				alert("服务器端异常!");
			}
		});
	}
	var t1=setInterval(function(){
		loadLog();
	},500);
	setInterval(function(){
		$.ajax({
			type:"get",
	        url:"servlet/TaskManagerServlet",
			data:{
	        	cmd:"getRunlingTasks",
			},
			dataType:"text",
			success:function(data){
				if(data){
					$(data).find("span").show().end().find("a").hide();
					$(".status:not("+data+")").find("span").hide().end().find("a").show()
				}
			},
			error:function(){
				alert("服务器端异常!");
			}
		});
	},100);
	</script>
  </head>
  
  <body>
  	<div id="log">正在加载日志...</div>
   	<div class="bar">
   		<form action="servlet/TaskManagerServlet" method="post">
   			<input type="hidden" value="reload" name="cmd">
    		<input type="submit" value="重新加载任务调度类" onclick="reloadTaskClass()">
    		<input type="button" value="显示(关闭)控制台" onclick="showLog()">
    		<input type="button" value="控制台最大(最小)化" onclick="maxShowConsole()">
   		</form>
   	</div>
    <div class="taskList">
	    <table>
	    	<tfoot>
	    		<tr>
	    			<td colspan="5"><span style="color: red;font-size: 24px;">请不要在任务正常执行的时间到达后一段时间内执行任务</span></td>
	    		</tr>
	    	</tfoot>
	    	<thead>
		    	<tr>
		    		<th>ID</th>
		    		<th>类名</th>
		    		<th>路径</th>
		    		<th>描述</th>
		    		<th>方法</th>
		    	</tr>
	    	</thead>
	    	<tbody>
	    		<c:forEach items="${tcbs}" var="tcb">
		    		<tr>
		    			<td>${tcb.id }</td>
		    			<td>${tcb.className }</td>
		    			<td>${tcb.cls }</td>
		    			<td>${tcb.remark }</td>
		    			<td>
		    				<table class="taskMethodTable">
		    					<thead>
		    						<tr>
		    							<th>执行</th>
		    							<th>方法名</th>
		    							<th>方法备注</th>
		    							<th>cron</th>
		    							<th>zone</th>
		    							<th>fixedDelay</th>
		    							<th>fixedDelayString</th>
		    							<th>fixedRate</th>
		    							<th>fixedRateString</th>
		    							<th>initialDelay</th>
		    							<th>initialDelayString</th>
		    						</tr>
		    					</thead>
		    					<tbody>
		    						<c:choose>
		    							<c:when test="${empty tcb.taksMethods }">
		    								<tr>
		    									<td colspan="10" class="notHaveDate">无任务调度方法</td>
		    								</tr>
		    							</c:when>
		    							<c:otherwise>
				    						<c:forEach items="${tcb.taksMethods }" var="tm">
				    							<tr>
				    								<td class="status status_${tcb.className }_${tm.name}" className="${tcb.className }" methodName="${tm.name }">
				    									<c:set var="isRuning" value="false"/>
				    									<c:set var="runId" value="${tcb.className }_${tm.name }"/>
				    									<c:forEach items="${runingTaskIds }" var="rti">
				    										<c:if test="${rti==runId }">
						    									<c:set var="isRuning" value="true"/>
				    										</c:if>
				    									</c:forEach>
				    									<c:choose>
				    										<c:when test="${isRuning }">
				    											<span style="color: red;">执行中</span>
							    								<a href="JavaScript:execMethod('${tcb.className }','${tm.name }')" style="display: none;">执行</a>
				    										</c:when>
				    										<c:otherwise>
				    											<span style="color: red;display: none;">执行中</span>
							    								<a href="JavaScript:execMethod('${tcb.className }','${tm.name }')">执行</a>
				    										</c:otherwise>
				    									</c:choose>
				    								</td>
				    								<td>${tm.name }</td>
				    								<td>${tm.remark }</td>
				    								<td>${tm.cron }</td>
				    								<td>${tm.zone }</td>
				    								<td>${tm.fixedDelay }</td>
				    								<td>${tm.fixedDelayString }</td>
				    								<td>${tm.fixedRate }</td>
				    								<td>${tm.fixedRateString }</td>
				    								<td>${tm.initialDelay }</td>
				    								<td>${tm.initialDelayString }</td>
				    							</tr>
				    						</c:forEach>
		    							</c:otherwise>
		    						</c:choose>
		    					</tbody>
		    				</table>
		    			</td>
		    		</tr>
	    		</c:forEach>
	    	</tbody>
	    </table>
    </div>
  </body>
</html>
